package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IPermissionService;
import cn.wolfcode.crm.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Collections;

@Controller
@RequestMapping("permission")
public class PermissionController {
    @Autowired
    private IPermissionService permissionService;
    //只是负责转发到角色页面
    @RequestMapping("view")
    public String view(){
        return "permission";
    }
    /*加载数据的列表页面*/
    @RequestMapping("query")
    @ResponseBody
    public PageResult query(QueryObject qo){
        PageResult pageResult = permissionService.query(qo);
        return pageResult;
    }

    //查询所有的权限信息
    @RequestMapping("selectAllNames")
    @ResponseBody
    public PageResult selectAllNames(){
        return permissionService.selectAllNames();
    }

    //根据角色id查询对应权限信息
    @RequestMapping("queryByRoleId")
    @ResponseBody
    public PageResult queryByRoleId(Long roleId){
        if (roleId != null) {
            return permissionService.queryByRoleId(roleId);
        }
        return new PageResult(0,Collections.EMPTY_LIST);
    }

    //加载权限数据
    @RequestMapping("reload")
    @ResponseBody
    public JSONResult reload(){
        permissionService.reload();
        return JSONResult.success();
    }
}
